﻿$(document).ready(function () {
    page.userControl = new NDUAP.Selector({ divID: "userId", typeID: 1, totalSelectNum: 100, quickBoxPositionX: -285, width: 450, searchData: page.userList, showMoreBtn: true });
    page.init();
});

var page = {
    url: "/handler/reward.ashx?action=",
    editor: null,

    init: function () {
        $(".top-bar .right li:eq(1)").addClass("current");
        $("#btnSubmit").bind("click", page.submit);

        KindEditor.ready(function (K) {
            page.editor = K.create('textarea[id="description"]', {
                minWidth: 600,
                items: [
		            'source', '|', 'undo', 'redo', '|', 'preview', 'template', 'cut', 'copy', 'paste',
		            'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
		            'justifyfull', 'insertorderedlist', 'insertunorderedlist',
                    'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
		            'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
		            'italic', 'underline', 'lineheight', 'removeformat', '|', 'image',
                    'table', 'hr', 'emoticons', 'link', 'unlink'
	            ],
                uploadJson: "/handler/upload.ashx?action=uploadFile"
            });
        });

        $("#amount").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");

        $("#type").bind("change", function () {
            var value = $(this).val();
            if (value == 1) {
                $("#amountTip").html("当前您的可用素币为 <strong style='color:red'>" + page.currentUser.available + "</strong>");
            } else if (value == 2) {
                $("#amountTip").html("当前您的特殊素币为 <strong style='color:red'>" + page.currentUser.honor + "</strong>");
            }
        });

        $("input[name='type']").bind("click", function () {
            if ($("input[name='type']:checked").val() == "1")
                $("#userId").show();
            else {
                $("#userId").hide();
            }
        });
    },

    submit: function () {
        var title = $.trim($("#title").val());
        if (title == "") {
            alert("请输入主题");
            $("#title").focus();
            return false;
        }

        var description = page.editor.html();
        if (description == "") {
            alert("请输入内容");
            page.editor.focus();
            return false;
        }

        var end = $("#end").val();
        if ($.trim(end) == "") {
            alert("请选择截止时间");
            $("#end").focus();
            return false;
        }

        var type = $("#type").val();
        if (type == "") {
            alert("请选择素币类型");
            $("#type").focus();
            return false;
        }

        var amount = $("#amount").val();
        if ($("#amount").val() == "") {
            alert("请输入素币数");
            $("#amount").focus();
            return false;
        }

        if ((type == 1 && amount > page.currentUser.available) || (type == 2 && amount > page.currentUser.honor)) {
            alert("素币数超过你的限额");
            $("#amount").focus();
            return false;
        }

        var userList = [];
        if ($("input[name='type']:checked").val() == 1) {
            var choose = page.userControl.getValue();
            if (choose.length == 0) {
                alert("非公开悬赏请选择可参与人员名单");
                return false;
            }

            for (var i = 0; i < choose.length; i++) {
                userList.push(choose[i].id);
            }
        }

        $.post(page.url + "add", { title: title, description: description, type: type, amount: amount, userList: userList.join(','), end: end }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            window.location.href = "Detail.aspx?id=" + data.id;
        }, "json");
    }
};

